package com.itheima.controller;

import com.itheima.Result.Result;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.service.impl.EmpServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/emps")
@Api(tags="员工管理")
public class EmpController {

    private final EmpService empService;

    /**
     * 查询员工信息
     */
    @ApiOperation(value = "根据id查询员工信息")
    @GetMapping("/{id}")
    public Result<Emp> empById(@PathVariable("id") Integer id) {
        log.info("查询id为{}的员工信息", id);
        return Result.success(empService.empById(id));
    }

    /**
     * 员工分页查询
     */
    @ApiOperation(value = "员工分页查询")
    @GetMapping()
    public Result<PageBean> pageQuery(String name,
                                      Integer gender,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "3") Integer pageSize,
                                      @DateTimeFormat(pattern = "yy-MM-dd") LocalDateTime begin,
                                      @DateTimeFormat(pattern = "yy-MM-dd") LocalDateTime end) {
        log.info("员工分页查询");

        return Result.success(empService.empList(name, gender, page, pageSize, begin, end));
    }

    /**
     * 批量删除员工信息
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除员工信息")
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable("ids") List<Integer> ids) {
        log.info("删除id为{}员工信息", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 添加员工信息
     * @param emp
     * @return
     */
    @ApiOperation(value = "添加员工信息")
    @PostMapping()
    public Result addEmp(@RequestBody Emp emp) {
        log.info("添加员工信息");
        empService.addEmp(emp);
        return Result.success();
    }

    /**
     * 更新员工信息
     * @param emp
     * @return
     */
    @ApiOperation(value = "更新员工信息")
    @PutMapping
    public Result UpdateEmp(@RequestBody Emp emp) {
        log.info("更新id为{}员工信息", emp.getId());
        empService.updateEmp(emp);
        return Result.success();
    }


}
